//
//  ThirdViewController.m
//  BeanTrip
//
//  Created by dlios on 15-6-29.
//  Copyright (c) 2015年 PandaGroup. All rights reserved.
//

#import "ThirdViewController.h"
#import "MBProgressHUD.h"

#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface ThirdViewController ()<UITextFieldDelegate>

@end

@implementation ThirdViewController


- (void)dealloc
{
    
    [_myTableView release];
    [_specialOfferArray release];
    [_HUD release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"特价";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];

    //请求值
    self.next_Page = 1;
    // 给定属性一个初值
    self.isUpLoading = NO;
    //声明数组
    self.specialOfferArray = [NSMutableArray array];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) style:UITableViewStyleGrouped];
    self.myTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/3)];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    //轮播图
    [self createScrollView];
 
    // 调用俩个刷新方法
    [self addHeader];
    [self addFooter];
    //菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"加载中";
    [self.HUD show:YES];
    //网络请求,参数表示从第几页开始请求数据
    [self getData:1];
    
    
}
//---------------------------------------------------------------------------

// 刷新头部
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block ThirdViewController *vc = self;
    
    // 添加下拉刷新头部控件
    [self.myTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        vc.next_Page = 1;
        vc.isUpLoading = NO;//标记为下拉操作
        [vc getData:1];//重新请求数据
        
    }];
    
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
//    [self.myTableView headerBeginRefreshing];
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block ThirdViewController *vc = self;
    // 添加上拉刷新尾部控件
    [self.myTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        vc.isUpLoading = YES;//标记为上拉操作
        [vc getData:vc.next_Page];//请求更多数据
        
    }];
}

-(void)getData:(NSInteger)pageNum
{
    //因为刷新功能需要变动的url
    NSString *url = [NSString stringWithFormat:@"http://breadtrip.com/tp/products/?category=&dest=&start=&depart=&page=%ld",pageNum];
    //根据page(或更多)变化的规律,更改变量
    self.next_Page++;
    //开始请求数据
    [HTTPTool get:url body:nil httpResult:JSON success:^(id result) {
        
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.specialOfferArray removeAllObjects];
        }
        NSMutableDictionary *dic1 = result;
        NSMutableArray *array1 = [dic1 objectForKey:@"products"];
        for (NSMutableDictionary *dic2 in array1) {
            SpecialOfferModel *specialOffer = [[SpecialOfferModel alloc] init];
            [specialOffer initWithDic:dic2];
            [self.specialOfferArray addObject:specialOffer];
            [specialOffer release];
        }
        //结束刷新
        [self.myTableView headerEndRefreshing];
        [self.myTableView footerEndRefreshing];
        //删除菊花,因为只用到一次,所以将它删除掉
        [self.HUD removeFromSuperview];
        [self.myTableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}
//设置区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.specialOfferArray.count;
}
//设置row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
//cell显示内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    ThirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[ThirdTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
//    //渐变效果
//    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
//    cell.alpha = 0;
//    [UIView beginAnimations:@"rotation" context:NULL];
//    [UIView setAnimationDuration:1];
//    cell.alpha = 1;
//    [UIView commitAnimations];
//    //cell动画
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    cell.mySpecialOffer = self.specialOfferArray[indexPath.section];
    
    return cell;
}
//row的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WIDTH/3+10;
}
//cell的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SpecialOfferDetialViewController *SpecialOfferDetialVC = [[SpecialOfferDetialViewController alloc] init];
    SpecialOfferDetialVC.specialOfferM = self.specialOfferArray[indexPath.section];
    [self.navigationController pushViewController:SpecialOfferDetialVC animated:YES];
    [SpecialOfferDetialVC release];
}

//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}



//创建轮播图的方法
-(void)createScrollView
{
    //接口中不带轮播图,我们从网络请求别的图片,当做轮播图
    NSMutableArray *imageArr = [NSMutableArray array];
    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"http://www.aomy.com/attach/2014-04/1398126537ahhb.jpg",@"image_path", nil];
    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"http://www.aomy.com/attach/2014-04/13981265338QK4.jpg",@"image_path", nil];
    NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"http://www.aomy.com/attach/2014-04/1398126532BeQE.jpg",@"image_path", nil];
    NSDictionary *dic4 = [NSDictionary dictionaryWithObjectsAndKeys:@"http://www.aomy.com/attach/2014-04/1398126531DRyK.jpg",@"image_path", nil];
    [imageArr addObject:dic1];
    [imageArr addObject:dic2];
    [imageArr addObject:dic3];
    [imageArr addObject:dic4];
    
    MyScrollView *myScrollV = [[MyScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/3)];
    [myScrollV setImages:imageArr];
    [self.myTableView.tableHeaderView addSubview:myScrollV];
    [myScrollV release];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
